package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.InteractionComment;
import com.atguigu.gmall.bean.InteractionVideo;
import com.atguigu.gmall.server.InteractionCService;
import com.atguigu.gmall.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class InteractionCController {
    @Autowired
    private InteractionCService interactionCService;

    @GetMapping("/comment")
    public String GetIC(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date==0){
            date= DateFormatUtil.now();
        }
        List<InteractionComment> interactionVideoList=interactionCService.ICList(date);

        StringBuilder jsonB=new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"id\": \"course_name\",\n" +
                "        \"name\": \"课程名称\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": \"avg_stars\",\n" +
                "        \"name\": \"用户平均评分\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"id\": \"user_count\",\n" +
                "        \"name\": \"评价用户数\"\n" +
                "      },\n" +
                "\t    {\n" +
                "        \"id\": \"comment_rate\",\n" +
                "        \"name\": \"好评率\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": [");
        for (int i = 0; i < interactionVideoList.size(); i++) {
            InteractionComment interactionComment = interactionVideoList.get(i);
            jsonB.append("{\n" +
                    "        \"course_name\": \""+interactionComment.getCourse_name()+"\",\n" +
                    "        \"user_count\": "+interactionComment.getUser_count()+",\n" +
                    "        \"avg_stars\": "+interactionComment.getAvg_stars()+",\n" +
                    "        \"comment_rate\": \""+interactionComment.getComment_rate()+"\"\n" +
                    "      }");
            if (i<interactionVideoList.size()-1){
                jsonB.append(",");
            }
        }
        jsonB.append("    ]\n" +
                "  }\n" +
                "}");
        return jsonB.toString();
    }
}
